//
//  ZYCTeaLeaveRecordRcvViewController.m
//  ZDSC
//
//  Created by 张威 on 2021/10/20.
//

#import "ZYCTeaLeaveRecordRcvViewController.h"
#import "ZYCTeaLeaveRecordRootCell.h"
#import "ZYCTeaLeaveRecordSendDetailViewController.h"
#import "ZYCTeaLeaveRecordApproveViewController.h"
#import "ZYCTeaLeaveRecordRcvHeaderView.h"
#import "ZYCNoDataView.h"
#import "ZYCTeaLeaveRecordRootModel.h"

static NSInteger pageSize = 10;
@interface ZYCTeaLeaveRecordRcvViewController ()<UITableViewDelegate,UITableViewDataSource,ZYCTeaLeaveRecordRcvHeaderViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) ZYCTeaLeaveRecordRcvHeaderView *headerView;
@property (nonatomic, strong) ZYCNoDataView *noDataView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, copy) NSString *receiveFlag;
@end

@implementation ZYCTeaLeaveRecordRcvViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.headerView];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(58.0f);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    self.receiveFlag = @"1";
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadNewData];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCTeaLeaveRecordRootCell *cell = [ZYCTeaLeaveRecordRootCell TeaLeaveRecordRootCellWithType:ZYCTeaLeaveRecordRootCellTypeRcv TableView:tableView];
    cell.model = self.dataSource[indexPath.section];
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01f;
    }
    return 10.f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}



- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYCTeaLeaveRecordRootModel *model = self.dataSource[indexPath.section];
    if ([model.status integerValue] == 1 && [self.receiveFlag integerValue] == 1) {
        ZYCTeaLeaveRecordApproveViewController *vc = [[ZYCTeaLeaveRecordApproveViewController alloc] init];
        vc.teacherLeaveId = model.teacherLeaveId;
        vc.taskId = model.taskId;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        ZYCTeaLeaveRecordSendDetailViewController *vc = [ZYCTeaLeaveRecordSendDetailViewController new];
        ZYCTeaLeaveRecordRootModel *model = self.dataSource[indexPath.section];
        vc.teacherLeaveId = model.teacherLeaveId;
        vc.taskId = model.taskId;
        [self.navigationController pushViewController:vc animated:YES];
    }

}

#pragma mark - 网络请求
- (void)loadNewData{
    self.pageNum = 1;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize),
        @"receiveFlag":self.receiveFlag
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_MyReceiveList requestService:RequestServiceActiviti parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                [self.dataSource removeAllObjects];
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCTeaLeaveRecordRootModel class] json:responseObject[@"body"][@"list"]];
                [self.dataSource removeAllObjects];
                [self.dataSource addObjectsFromArray:array];
                [self.tableView reloadData];
                [self reset];
                [self.tableView.mj_header endRefreshing];
                if (self.dataSource.count == 0) {
                    self.noDataView.hidden = NO;
                    self.tableView.hidden = YES;
                }else{
                    self.noDataView.hidden = YES;
                    self.tableView.hidden = NO;
                }
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }

        } failure:^(NSError * _Nonnull error) {
            [self.tableView.mj_header endRefreshing];
        }];
}

- (void)loadMoreData{
    self.pageNum ++;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize),
        @"receiveFlag":self.receiveFlag
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_MyReceiveList requestService:RequestServiceActiviti parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCTeaLeaveRecordRootModel class] json:responseObject[@"body"][@"list"]];
                if (array.count == 0) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.dataSource addObjectsFromArray:array];
                    [self.tableView reloadData];
                    [self.tableView.mj_footer endRefreshing];
                }
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }

        } failure:^(NSError * _Nonnull error) {
            [self.tableView.mj_footer endRefreshing];
        }];
    
}
- (void)reset{
    [self.tableView.mj_footer setRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_footer resetNoMoreData];
}

#pragma mark - ZYCTeaLeaveRecordRcvHeaderViewDelegate
- (void)teaLeaveRecordRcvHeaderViewDidReceiveFlag:(NSString *)receiveFlag{
    self.receiveFlag = receiveFlag;
    [self loadNewData];
}
#pragma mark - getters

- (ZYCTeaLeaveRecordRcvHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[ZYCTeaLeaveRecordRcvHeaderView alloc] init];
        _headerView.delegate = self;
    }
    return _headerView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

- (ZYCNoDataView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[ZYCNoDataView alloc] initWithFrame:self.tableView.frame];
        _noDataView.hidden = YES;
        [self.view addSubview:_noDataView];
    }
    return _noDataView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCTeaLeaveRecordRootCell class] forCellReuseIdentifier:NSStringFromClass([ZYCTeaLeaveRecordRootCell class])];
        _tableView.backgroundColor = HEX_RGB(@"#FAFAFA");
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 158.0f;
    }
    return _tableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
